<template>

  <div>

    <el-form  :model="form" label-width="80px">
      <!--		  <el-form-item label="用户ID">-->
      <!--		    <el-input v-model="form.userid" readonly></el-input>-->
      <!--		  </el-form-item> -->
      <el-form-item label="用户名">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="form.password" show-password></el-input>
      </el-form-item>

      <el-form-item label="昵称">
        <el-input   v-model="form.nickname"></el-input>
      </el-form-item>
      <el-form-item label="归属科室">
        <el-select  v-model="form.deptId"  placeholder="请选择" @change="chageDept">
          <el-option
            v-for="item in deptList"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="挂号级别">
        <el-select  v-model="form.registLevel"  placeholder="请选择">
          <el-option
            v-for="item in levelList"
            :key="item.registLevel"
            :label="item.caption"
            :value="item.registLevel">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="过期时间">
        <el-input v-model="form.expire"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="save()">保存</el-button>

      </el-form-item>
    </el-form>



  </div>

</template>

<script>
import {getForm,save,update,initEdit} from "@/api/sys/user";
export default{
  name:'DeptEdid',
  props:["id"],
  data(){
    return {
      deptList:[],
      levelList:[],
      form:{
        username:'',
        password:'',
        nickname:'',
        deptId:'',
        deptName:'',
        registLevel:'',
        expire:''
      }
    }
  },
  created() {
    //初始化
    this.initEdit()


    if(this.id){
      this.getForm();
    }
  },
  methods:{
    chageDept(deptId){
      this.deptList.forEach((item,ind)=>{
        if( item.deptId == deptId){
          this.form.deptName = item.deptName
        }
      })
    },
    //初始化页面的 两个下拉选  部门,   挂号级别
    initEdit(){
      initEdit().then(res=>{
        this.deptList =  res.deptList
        this.levelList =  res.levelList
      })
    },
    //修改的时候初始化使用
    getForm(){
      getForm(this.id).then((form) =>{
        this.form = form;
      }).catch((e)=>{
        console.log("失败的情况",e)
      });




    },
    save(){
      let promise  =  null;

      // delete  this.form.createtime;

      if(this.id) {
        promise = update( this.form);
      }else{
        promise = save( this.form);
      }
      // form /
      promise.then((success) =>{
        if(success){
          this.$message.success("保存成功");

          //关掉自己
          this.$emit("update:visible",false);
          this.$emit("close");
        }
      }).catch((e)=>{
        console.log("失败的情况",e)
      });

    }
  }
}


</script>

<style scoped>




</style>
